﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Forms.Integration;
using System.Windows.Input;
using System.Windows.Threading;
using CefSharp;
using CefSharp.Wpf;
using V8.Net;

namespace DreamSpace.Studio
{
    /// <summary>
    /// The bridge class is used to communicate to the host application from within JavaScript.
    /// </summary>
    public partial class StudioHostBridge : HostBridge
    {
        Window _MainWindow { get { return Application.Current.MainWindow; } }

        public static void UIInvoke(Action a)
        {
            Application.Current.Dispatcher.BeginInvoke((Action)(() =>
            {
                try { a(); }
                catch (Exception ex)
                {
                    MainWindow.AddConsoleLine(Exceptions.GetFullErrorMessage(new Exception("A host error has occurred:", ex)));
                }
            }));
        }

        public static T UIInvoke<T>(Func<T> f)
        {
            try { return (T)Application.Current.Dispatcher.Invoke(f); }
            catch (Exception ex)
            {
                throw new Exception(Exceptions.GetFullErrorMessage(new Exception("A host error has occurred:", ex), true), ex);
            }
        }

        public override bool isStudio() { return true; }
        public override bool isServer() { return false; }

        public override void setTitle(string title) { UIInvoke(() => { _MainWindow.Title = title ?? ""; }); }
        public override string getTitle() { return UIInvoke(() => { return _MainWindow.Title; }); }

    }
}
